package com.evildog.workspace.system.application.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.evildog.workspace.dependencies.mybatis.object.QueryConditionDTO;
import com.evildog.workspace.system.application.bo.DictDataListBO;
import com.evildog.workspace.system.application.convert.DictDataConvert;
import com.evildog.workspace.system.application.dto.DictDataDTO;
import com.evildog.workspace.system.application.dto.DictDataListDTO;
import com.evildog.workspace.system.application.entity.DictData;
import com.evildog.workspace.system.application.entity.DictDataListDO;
import com.evildog.workspace.system.application.mapper.DictDataMapper;
import com.evildog.workspace.system.application.service.DictDataService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictData> implements DictDataService {

    @Override
    public List<DictDataListBO> getList(List<QueryConditionDTO> queryCondition, DictDataListDTO dto) {
        List<DictDataListDO> dictDataList = this.baseMapper.getList(queryCondition, dto);
        return DictDataConvert.INSTANCE.convert(dictDataList);
    }

    @Override
    public List<DictDataListBO> getValidList(Long dictTypeId) {
        List<DictData> list = list(Wrappers.<DictData>lambdaQuery()
                .eq(DictData::getDictTypeId, dictTypeId)
        );

        return DictDataConvert.INSTANCE.convertBOList(list);
    }

    @Override
    public boolean save(DictDataDTO dto){
        DictData newDictData = DictDataConvert.INSTANCE.convert(dto);
        return save(newDictData);
    }

    @Override
    public boolean updateById(Long id, DictDataDTO dto){
        DictData oldDictData = getById(id);
        Assert.notNull(oldDictData, "dictData is null!");
        DictData newDictData = DictDataConvert.INSTANCE.convert(dto);
        newDictData.setId(id);
        return updateById(newDictData);
    }
}
